package com.springboot.Service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.springboot.Service.ChinaDayListService;
import com.springboot.domain.ChinaDay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author 权哥
 * @version 1.0
 * @date 2022/5/4 15:15
 */
@Service
public class ChinaDayListimpl implements ChinaDayListService {

    public ChinaDayListimpl() throws IOException {
    }
    //1.保存源地址
    String srcUrl="https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=chinaDayList,chinaDayAddList,nowConfirmStatis,provinceCompare";
    //2.模拟发送请求
    URLConnection oc=new URL(srcUrl).openConnection();
    //		3.获取输入流
    InputStream in=oc.getInputStream();
    //		4.创建一个字符流写入文件
    BufferedReader br=new BufferedReader(new InputStreamReader(in,"UTF-8"));
    //5.读取字符，一行一行的读取
    String all="";


    public static void main(String[] args) throws IOException {
//        ArrayList<ChinaDay> list=ChinaDayLis();
//        for(ChinaDay chinaDay: list)
//        {
//            System.out.println(chinaDay.getData());
//        }
    }

    @Override
    public ArrayList<ChinaDay> ChinaDayList() throws IOException {
        ArrayList<ChinaDay> ChinaDayList=new ArrayList<ChinaDay>();

        while(true)
        {
            //一次性读取一行数据
            String rl=br.readLine();
            if(rl==null)
            {
                break;//如果读取的内容为空，就跳出循环
            }
            all+=rl;
        }

        //解析json

        Object jsonObject=JSONObject.parse(all);
        @SuppressWarnings({ "unchecked", "rawtypes", "unused" })
        Map<String, Object> jsonMap=(Map)jsonObject;
        String data=jsonMap.get("data").toString();
        JSONObject json = (JSONObject) JSON.parse(data);


        String chinaDayListjson=json.getString("chinaDayList");
        JSONArray chinaDayList=(JSONArray) JSON.parse(chinaDayListjson);
        for(int i=0;i<chinaDayList.size();i++)
        {
            ChinaDay chinaDay=new ChinaDay();

            chinaDay.setDate(chinaDayList.getJSONObject(i).getString("date"));//日期
            chinaDay.setNowConfirm(Integer.parseInt(chinaDayList.getJSONObject(i).getString("nowConfirm")));//截止当日现有确诊人数
            chinaDay.setNoInfect(Integer.parseInt(chinaDayList.getJSONObject(i).getString("noInfect")));//截止当日现有无症状感染者人数
            chinaDay.setConfirm(Integer.parseInt(chinaDayList.getJSONObject(i).getString("confirm"))); //截止当日累计确诊人数
            chinaDay.setImportedCase(Integer.parseInt(chinaDayList.getJSONObject(i).getString("importedCase")));//截止当日累计境外
            chinaDay.setHeal(Integer.parseInt(chinaDayList.getJSONObject(i).getString("heal")));//截止当日累计治愈人数
            chinaDay.setDead(Integer.parseInt(chinaDayList.getJSONObject(i).getString("dead")));//截止当日累计死亡人数

            ChinaDayList.add(chinaDay);
        }

        return ChinaDayList;
    }
}
